﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class DiscountDA : BaseDAL, IDiscount
    {
        private const string tableName = "Discount";
        private const string bouquetID = "BouquetID";
        private const string discountPercent = "DiscountPercent";

        public DiscountDA()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region IDiscount Members

        public Discount[] GetDiscount()
        {
            Discount[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;

                    cmd.CommandText = String.Format("SELECT * FROM {0}", tableName);

                String[] columnNames = {    bouquetID,
                                            discountPercent};
                result = SelectCollection<Discount>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public Discount GetDiscount(int _bouquetID)
        {
            Discount[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;

                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2}",
                        tableName, bouquetID, bouquetID);
                    cmd.Parameters.Add(String.Format("@{0}", bouquetID), SqlDbType.Int).Value = _bouquetID;
                

                String[] columnNames = {    bouquetID,
                                            discountPercent};
                result = SelectCollection<Discount>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (result.Length > 0)
                return result[0];
            else
                return null;
        }

        public int InsertDiscount(Discount _discount)
        {
            int result = 0;
            try
            {
                String[] columnNames = {    bouquetID,
                                            discountPercent};

                Object[] values = { _discount.BouquetID,
                                    _discount.DiscountPercent};
                int autoNumber;
                result = BaseDAL.InsertTableWithReturnID(tableName, columnNames, values, out autoNumber);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public int UpdateDiscount(Discount _discount)
        {
            int result = 0;
            try
            {
                String[] columnNames = { discountPercent};
                Object[] values = { _discount.DiscountPercent};
                String[] keyColumnNames = { bouquetID };
                Object[] keyColumnValues = { _discount.BouquetID };
                result = UpdateTable(tableName, columnNames, values, keyColumnNames, keyColumnValues);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        #endregion
    }
}